package com.wf.smartcomm.server.admin.action;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.aspectj.util.FileUtil;
import org.json.JSONObject;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ModelDriven;
import com.wf.smartcomm.server.bean.Guide;
import com.wf.smartcomm.server.bean.Image;
import com.wf.smartcomm.server.bean.ThumbImage;
import com.wf.smartcomm.server.bean.User;
import com.wf.smartcomm.server.service.GuideService;
import com.wf.smartcomm.server.service.ImageService;
import com.wf.smartcomm.server.service.ThumbImageService;
import com.wf.smartcomm.server.util.IdUtils;
import com.wf.smartcomm.server.util.ImageUtils;
import com.wf.smartcomm.server.util.PushUtils;
import com.wf.smartcomm.server.util.StringUtil;

@Controller(value="adminGuideAction")
@Scope("prototype")
public class GuideAction extends BaseAction implements ModelDriven<Guide>{

	 	private List<File> upload;  
	    // /多个上传文件的类型集合  
	    private List<String> uploadContentType;  
	   // 多个上传文件的文件名集合  
	    private List<String> uploadFileName;  
	private Guide guide = new Guide();
	@Resource
	private GuideService guideServiceImpl;
	
	public List<File> getUpload() {
		return upload;
	}

	public void setUpload(List<File> upload) {
		this.upload = upload;
	}

	public List<String> getUploadContentType() {
		return uploadContentType;
	}

	public void setUploadContentType(List<String> uploadContentType) {
		this.uploadContentType = uploadContentType;
	}

	public List<String> getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(List<String> uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public void save() throws Exception{
		
		User user = (User) session.getAttribute("loginUser");
		guide.setVillage(user.getLoginVillage());
		
		if (upload != null && upload.size() > 0) {
			for (File file : upload) {
				String fileName = IdUtils.uuid()+".png";
				String path = request.getServletContext().getRealPath(
						"images");
				String url = "http://"+PushUtils.pushConfig.getProperty("ImageServerIp");
				if (request.getServerPort() != 80) {
					url+=":"+request.getServerPort();
				}
				url+=request.getContextPath()+"/images/";
				guide.setImageUrl(url+fileName);
				File des = new File(path + File.separator + fileName);
				FileUtil.copyFile(file, des);
				
				ImageUtils.getThumbnail(des.getAbsolutePath(),
						request.getServletContext().getRealPath(
								"thumbImages")+File.separator+fileName, 150, 100);
				
			}
		
		}
		
		if (!StringUtil.isEmpty(guide.getObjectId())) {
			if (StringUtil.isEmpty(guide.getImageUrl())) {
				guide.setImageUrl(guideServiceImpl.get(guide.getObjectId()).getImageUrl());
			}
			guide.setCreatedAt(String.valueOf(System.currentTimeMillis()));
			guideServiceImpl.update(guide);
		}else {
			guideServiceImpl.save(guide);
		}
		printJsonData(200);
	}
	
	public void list() throws Exception {
		String aoData = request.getParameter("aoData");
		String sEcho = "";
		String iDisplayStart = "";
		String iDisplayLength = "";
		String sSearch = "";
		
		JSONObject jo = new JSONObject(aoData);
		sEcho = jo.getString("sEcho");
		iDisplayStart = jo.getString("iDisplayStart");
		iDisplayLength = jo.getString("iDisplayLength");
		sSearch = jo.getString("sSearch");
		User user = (User) session.getAttribute("loginUser");
		page.setBeginIndex(Integer.parseInt(iDisplayStart));
		page.setPageSize(Integer.parseInt(iDisplayLength));
		if (!StringUtil.isEmpty(sSearch)) {
			guideServiceImpl.query(user.getLoginVillage(),sSearch, page);
		}else {
			guideServiceImpl.listByVillage(user.getLoginVillage(), page);
		}
		
		int  initEcho = Integer.parseInt(sEcho)+1;
		int count = page.getTotalCount();
		HashMap<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("iTotalRecords", count);
		dataMap.put("sEcho",initEcho);
		dataMap.put("iTotalDisplayRecords", count);
		dataMap.put("aaData", page.getDataList());
		printJsonData(outGson.toJson(dataMap));
	}
	
	public String edit() throws Exception{
		guide = guideServiceImpl.get(guide.getObjectId());
		request.setAttribute("guide", guide);
		return "edit";
	}
	
	public void delete() throws Exception{
		guideServiceImpl.delete(guide);
		printJsonData(200);
	}
	
	@Override
	public Guide getModel() {
		// TODO Auto-generated method stub
		return guide;
	}

}
